import * as Util from "../../core/Util";
//
export class tileLayer {
  options = {
    // @option minZoom: Number = 0
    // The minimum zoom level down to which this layer will be displayed (inclusive).
    minZoom: 0,

    // @option maxZoom: Number = 18
    // The maximum zoom level up to which this layer will be displayed (inclusive).
    maxZoom: 18,

    // @option subdomains: String|String[] = 'abc'
    // Subdomains of the tile service. Can be passed in the form of one string (where each letter is a subdomain name) or an array of strings.
    subdomains: "abc",

    // @option errorTileUrl: String = ''
    // URL to the tile image to show in place of the tile that failed to load.
    errorTileUrl: "",

    // @option zoomOffset: Number = 0
    // The zoom number used in tile URLs will be offset with this value.
    zoomOffset: 0,

    // @option tms: Boolean = false
    // If `true`, inverses Y axis numbering for tiles (turn this on for [TMS](https://en.wikipedia.org/wiki/Tile_Map_Service) services).
    tms: false,

    // @option zoomReverse: Boolean = false
    // If set to true, the zoom number used in tile URLs will be reversed (`maxZoom - zoom` instead of `zoom`)
    zoomReverse: false,

    // @option detectRetina: Boolean = false
    // If `true` and user is on a retina display, it will request four tiles of half the specified size and a bigger zoom level in place of one to utilize the high resolution.
    detectRetina: false,

    // @option crossOrigin: Boolean|String = false
    // Whether the crossOrigin attribute will be added to the tiles.
    // If a String is provided, all tiles will have their crossOrigin attribute set to the String provided. This is needed if you want to access tile pixel data.
    // Refer to [CORS Settings](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes) for valid String values.
    crossOrigin: false,

    // @option referrerPolicy: Boolean|String = false
    // Whether the referrerPolicy attribute will be added to the tiles.
    // If a String is provided, all tiles will have their referrerPolicy attribute set to the String provided.
    // This may be needed if your map's rendering context has a strict default but your tile provider expects a valid referrer
    // (e.g. to validate an API token).
    // Refer to [HTMLImageElement.referrerPolicy](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/referrerPolicy) for valid String values.
    referrerPolicy: false,
  };
  constructor() {
    this._url =
      // "http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}"; // 高德
     "http://192.168.0.25:6163/igs/rest/mrms/tile/ZCQC5300002021GYX//{z}/{y}/{x}" // arcgis
      // "http://192.168.0.25:6163/igs/rest/mrms/tile/ZCQC5300002021GYX//{z}/{y}/{x}";
      // "https://map.geoq.cn/arcgis/rest/services/ChinaOnlineStreetWarm/MapServer/tile/{z}/{y}/{x}"
    // "http://api0.map.bdimg.com/customimage/tile?&x={x}&y={y}&z={z}&udt=20191205&scale=1&ak=5ieMMexWmzB9jivTq6oCRX9j"
  }

  getTileUrl(coords) {
    const data = {
      r: "",
      s: this._getSubdomain(coords),
      x: coords.x,
      y: coords.y,
      z: coords.z,
    };
    if (this._map && !this._map.options.crs.infinite) {
      const invertedY = this._globalTileRange.max.y - coords.y;
      if (this.options.tms) {
        data["y"] = invertedY;
      }
      data["-y"] = invertedY;
    }
    return Util.template(this._url, Util.extend(data, this.options));
  }
  _getSubdomain(tilePoint) {
    const index =
      Math.abs(tilePoint.x + tilePoint.y) % this.options.subdomains.length;
    return this.options.subdomains[index];
  }
  _getZoomForUrl() {
    let zoom = this._tileZoom;
    const maxZoom = this.options.maxZoom,
      zoomReverse = this.options.zoomReverse,
      zoomOffset = this.options.zoomOffset;

    if (zoomReverse) {
      zoom = maxZoom - zoom;
    }
    return zoom + zoomOffset;
  }
}

